import types from './types'
import lodash from 'lodash'

export default {
  [types.SET_ROLE] (state, payload) {
    state.role = payload
  },
  [types.SET_ROLES] (state, payload) {
    state.roles = payload
  },
  [types.ADD_ROLE] (state, payload) {
    state.roles.items.push(payload)
    state.roles.total ++
  },
  [types.DELETE_ROLE] (state, payload) {
    let index = lodash.findIndex(state.roles.items, ['id', payload])
    if (index >= 0) {
      state.roles.items.splice(index, 1)
    }
  },
  [types.SET_ROLE_LIST] (state, payload) {
    state.roleList = payload
  },
  [types.SET_ROLE_PERMS] (state, payload) {
    state.perms = payload
  },
  [types.ADD_ROLE_PERM] (state, payload) {
    state.perms.push(payload)
  },
  [types.DELETE_ROLE_PERM] (state, payload) {
    let index = lodash.findIndex(state.perms, ['id', payload])
    if (index >= 0) {
      state.perms.splice(index, 1)
    }
  }
}
